﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DownLoad.Helpers
{
    public class CmdHelper
    {
        /// <summary>
        /// 拷贝超长路径的文件
        /// </summary>
        /// <param name="sharedPath">源路径</param>
        /// <param name="localPath">目标路径</param>
        /// <param name="msg">返回信息</param>
        public static void CopyFilesFromShareToLocal(string sharedPath, string localPath,out string msg)
        {
            // 构造copy命令
            string command = $"copy \"{sharedPath}\" \"{localPath}\"";
            RunCmd(command,out msg);
           
        }
        /// <summary>
        /// 执行CMD命令
        /// </summary>
        /// <param name="cmd">CMD命令</param>
        /// <param name="output">输出结果信息</param>
        public static void RunCmd(string cmd, out string output)
        {
            // 配置 ProcessStartInfo
            ProcessStartInfo processInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = $"/c {cmd}",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            // 启动进程
            using (Process process = new Process { StartInfo = processInfo })
            {
                process.Start();
                output = process.StandardOutput.ReadToEnd();
                process.WaitForExit();
                process.Close();
            }
        }
    }
}
